package io;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class SeanKeyListener implements KeyListener {
	//array of key states made as integers for more possible states. 
		private int[] keys = new int[256];
		
		//one for each ascii character.
		private boolean[] key_state_up = new boolean[256];  //true if pressed
		private boolean[] key_state_down = new boolean[256]; //true if not pressed
		
		private static SeanKeyListener instance = new SeanKeyListener();

		
		protected SeanKeyListener() {
			
		}
		
		/**
		 * Singleton accessor the only means of getting the instantiated object.
		 * @return One and only InputManager object.
		 */
		public static SeanKeyListener getInstance() {
			return instance;
		}

		public void keyPressed(KeyEvent e) {
		//	System.out.println("InputManager: A key has been pressed code=" + e.getKeyCode());
			if( e.getKeyCode() >= 0 && e.getKeyCode() < 256 ) {
				keys[e.getKeyCode()] = (int) System.currentTimeMillis();
				key_state_down[e.getKeyCode()] = true;
				key_state_up[e.getKeyCode()] = false;
			}
		}

		/**
		 * This function is specified in the KeyListener interface. It sets the 
		 * state elements for whatever key was released.
		 * 
		 * @param e The KeyEvent fired by the awt Toolkit.
		 */
		public void keyReleased(KeyEvent e) {
			//System.out.println("InputManager: A key has been released code=" + e.getKeyCode());
			if( e.getKeyCode() >= 0 && e.getKeyCode() < 256 ) {
				keys[e.getKeyCode()] = 0;
				key_state_up[e.getKeyCode()] = true;
				key_state_down[e.getKeyCode()] = false;
			}
		}

		/**
		 * This function is called if certain keys are pressed namely those used
		 * for text input.
		 * 
		 * @param e The KeyEvent fired by the awt Toolkit.
		 */
		public void keyTyped(KeyEvent e) {	
			
		}
		
		/**
		 * Returns true if the key (0-256) is being pressed use the KeyEvent.VK_
		 * key variables to check specific keys.
		 * 
		 * @param key The ascii value of the keyboard key being checked
		 * @return true is that key is currently being pressed.
		 */
		public boolean isKeyDown( int key ) {
			return key_state_down[key];
		}
		
		/**
		 * Returns true if the key (0-256) is being pressed use the KeyEvent.VK_
		 * key variables to check specific keys.
		 * 
		 * @param key The ascii value of the keyboard key being checked
		 * @return true is that key is currently being pressed.
		 */
		public boolean isKeyUp( int key ) {
			return key_state_up[key];
		}
		
		/**
		 * Only resets the key state up because you don't want keys to be showing
		 * as up forever which is what will happen unless the array is cleared.
		 */
		public void update() {
			//clear out the key up states
			key_state_up = new boolean[256];
		}
}
